package com.example.happ.controller;


import com.example.happ.pojo.Merchant;
import com.example.happ.pojo.MerchantAccount;
import com.example.happ.pojo.MerchantMain;
import com.example.happ.pojo.Store;
import com.example.happ.service.MerchantInfoService;
import com.example.happ.service.ReviewInfoService;
import com.example.happ.service.StoreService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
public class mainController {
    @Resource
    private StoreService storeService;
    @Resource
    private ReviewInfoService reviewInfoService;
    @Resource
    private MerchantInfoService merchantInfoService;
    @RequestMapping("/main")//商家主页面请求
    @CrossOrigin(origins = "http://localhost:8080")
    public MerchantMain responseMerchantById(@RequestBody Map<String, String> requestBody){
        System.out.println("商家主页面请求收到的数据："+requestBody);
        int id= Integer.parseInt(requestBody.get("id"));
        int count=merchantInfoService.countReviewById(id);//返还待审核条数
        Merchant merchant=merchantInfoService.selectAmountById(id);//返还商户所有信息
        Map<String, Integer> request = new HashMap<>();
        request.put("type", Integer.valueOf(requestBody.get("type")));
        request.put("id",Integer.valueOf(id));
        MerchantAccount merchantAccount = selectValidReview(request);
        System.out.println("商家主页面请求返回的数据："+new MerchantMain(merchant,merchantAccount,count));
        return new MerchantMain(merchant,merchantAccount,count);
    }
    @RequestMapping("/select")//返还商家消耗金额与审核条数
    @CrossOrigin(origins = "http://localhost:8080")
    public MerchantAccount selectValidReview(@RequestBody Map<String, Integer> requestBody){
        System.out.println("返还商家消耗金额与审核条数请求收到的数据"+requestBody);
        int type=Integer.valueOf(requestBody.get("type"));
        int merchantId=Integer.valueOf(requestBody.get("id"));
        Store store=storeService.findStore(merchantId);
        double consume=reviewInfoService.countConsumeReviewById(type,store.getId());
        long status=reviewInfoService.countValidReviewById(type,store.getId());
        System.out.println("返还商家消耗金额与审核条数请求返回的数据"+new MerchantAccount(consume,status));
        return new MerchantAccount(consume,status);
    }
    @RequestMapping("/app/amount")//添加修改商户金额
    @CrossOrigin(origins = "http://localhost:8080")
    public boolean updateValidReview(@RequestBody Map<String, Integer> requestBody){
        System.out.println("商家修改金额请求收到的数据"+requestBody);
        boolean success = false;
        Double amount= Double.valueOf(requestBody.get("amount"));
        int merchantId=requestBody.get("id");
        success =merchantInfoService.updateMerchantAmountInfo(amount,merchantId);
        return success;
    }
    @RequestMapping("/app/find")//商家信息第一次请求，返回商家信息
    @CrossOrigin(origins = "http://localhost:8080")
    public Merchant getMerchant(@RequestBody Map<String, Integer> requestBody, HttpServletRequest request){
        if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
            return null; // 直接返回 null，不处理预检请求
        }
        System.out.println("商家信息请求收到的数据"+requestBody);
        Merchant merchant=merchantInfoService.findAll(requestBody.get("id"));
        System.out.println("商家信息请求返回的数据"+merchant);
        return merchant;
    }

    @RequestMapping("/app/update")//商家信息保存请求
    @CrossOrigin(origins = "http://localhost:8080")
    public int updateMerchantInfo(@RequestBody Map<String, String> requestBody, HttpServletRequest request) {
        if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
            return -1; // 直接返回 null，不处理预检请求
        }
        Merchant merchant =new Merchant();
        System.out.println("商家信息保存请求收到的数据"+requestBody);
        int result = merchantInfoService.updateMerchantInfo(merchant,requestBody);
        return result;
    }
}
